package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.entity.AlertData;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.zzyl.intercept.AutoFillInterceptor.loadUserId;

/**
 * @Description AlertDataServiceImpl
 * @Author lyh
 * @Date 2024-11-04
 */
@Service
public class AlertDataServiceImpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Override
    public PageResponse<AlertDataVo> page(Integer pageNum, Integer pageSize, String deviceName, Long startTime, Long endTime, Long id, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        LocalDateTime startDate=null;
        LocalDateTime endDate=null;
        if(startTime!=null) {
           startDate = LocalDateTimeUtil.of(startTime);
        }
        if(endTime!=null) {
            endDate = LocalDateTimeUtil.of(endTime);
        }
       Page<AlertDataVo> page=alertDataMapper.findByCondition(deviceName,startDate,endDate,id,status);
        return PageResponse.of(page,AlertDataVo.class);
    }

    @Override
    public void handleAlertData(AlertDataHandleDto alertDataHandleDto, Long id) {
        AlertData alertData = BeanUtil.toBean(alertDataHandleDto, AlertData.class);
        alertData.setId(id);
        alertData.setProcessorId(UserThreadLocal.getMgtUserId());
        alertData.setStatus(1);
        alertDataMapper.updateByPrimaryKeySelective(alertData);
    }
}
